package com.smritimap.demo.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

/**
 * Created by zxn on 2024/12/17.
 */
public class CanvasRotateView extends View {


    public CanvasRotateView(Context context) {
        super(context);
        initPaint();
    }

    public CanvasRotateView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initPaint();
    }

    public CanvasRotateView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initPaint();
    }

    private final Paint paint = new Paint();

    private void initPaint() {
        paint.setStrokeWidth(3);
    }

    @Override
    protected void onDraw(@NonNull Canvas canvas) {
        super.onDraw(canvas);

        //drawLine(canvas);

        //drawLine1(canvas);

        //drawLine2(canvas);

        //drawLine3(canvas);

        //drawLine4(canvas);

        //draw5(canvas);

        drawLine6(canvas);
    }

    private void drawLine6(Canvas canvas) {
        canvas.translate(getWidth() / 2F, getHeight() / 2F);
        canvas.rotate(90);
        canvas.drawLine(0, 0,  0, 300, paint);
    }

    private void draw5(Canvas canvas) {
        // 移动画布到中心点进行旋转
        canvas.translate(getWidth() / 2F, getHeight() / 2F);
        canvas.rotate(45); // 旋转画布
        // 绘制一个矩形
        canvas.drawRect(-100, -100, 100, 100, paint);
    }

    private void drawLine4(Canvas canvas) {
        canvas.save();
        float cX = getWidth() / 2F;
        float cY = getHeight() / 2F;
        float stopX = cX;
        float startY = cY - 200;
        float stopY = cY + 200;
        // 移动画布到中心点进行旋转
        canvas.drawLine(cX, startY, stopX, stopY, paint);
        canvas.translate(getWidth() / 2F, getHeight() / 2F);
        canvas.rotate(45);
        canvas.restore();
    }

    private void drawLine3(Canvas canvas) {
        canvas.save();
        //顺时针旋转、
        canvas.rotate(45f);
        canvas.drawLine(0, 0, 300, 300, paint);
        canvas.restore();
    }

    private void drawLine2(Canvas canvas) {
        canvas.save();
        canvas.skew(1F, 0f);
        canvas.drawLine(0, 0, 300, 300, paint);
        canvas.restore();
    }

    private void drawLine1(Canvas canvas) {
        float cX = getWidth() / 2F;
        float cY = getHeight() / 2F;
        float startX3 = cX - 100;
        float stopX4 = cX + 100;
        canvas.save();
        canvas.drawLine(startX3, cY, stopX4, cY, paint);
        canvas.restore();
    }

    private void drawLine(Canvas canvas) {
        canvas.save();
        float cX = getWidth() / 2F;
        float cY = getHeight() / 2F;
        float stopX = cX;
        float startY = cY - 200;
        float stopY = cY + 200;
        //float startX, float startY,
        canvas.skew(0.5F, 0f);
        canvas.drawLine(cX, startY, stopX, stopY, paint);
        canvas.restore();
    }
}
